﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Data
{
  public  class ChiTietBaoCaoTonRepository
    {
      public static List<ChiTietBaoCaoTonDTO> GetChiTietByMaBaoCao(int maBaoCao)
      {
          NhaSachEntities context = new NhaSachEntities();
          var q = (from ct in context.ChiTietBaoCaoTonList
                   where ct.MaBaoCaoTon == maBaoCao
                   orderby ct.MaSach ascending
                   select new ChiTietBaoCaoTonDTO()
                   {
                       MaChiTiet = ct.MaChiTiet,
                       MaSach = (int)ct.MaSach,
                       MaBaoCaoTon = (int)ct.MaBaoCaoTon,
                       TonDau = (int)ct.TonDau,
                       TonCuoi = (int)ct.TonCuoi,
                       PhatSinh = (int)ct.PhatSinh
                   }).DefaultIfEmpty();
          
          return q.ToList();
      }
      public static ChiTietBaoCaoTonDTO GetChiTietBaoCao(int maBaoCao, int maSach)
      {
          NhaSachEntities context = new NhaSachEntities();
          var q = (from ct in context.ChiTietBaoCaoTonList
                   where ct.MaBaoCaoTon == maBaoCao && ct.MaSach == maSach
                   select new ChiTietBaoCaoTonDTO()
                   {
                       MaChiTiet=ct.MaChiTiet,
                       MaSach=(int)ct.MaSach,
                       MaBaoCaoTon=(int)ct.MaBaoCaoTon,
                       TonDau=(int)ct.TonDau,
                       TonCuoi=(int)ct.TonCuoi,
                       PhatSinh=(int)ct.PhatSinh
                   }).FirstOrDefault();
          return q;
      }
      public static bool Exists(int maBaoCao, int maSach)
      {
          NhaSachEntities context = new NhaSachEntities();

          var q = (from ct in context.ChiTietBaoCaoTonList
                   where ct.MaBaoCaoTon == maBaoCao && ct.MaSach == maSach
                   select ct).Count();
          if (q > 0)
              return true;
          return false;
      }
      public static void Insert(ChiTietBaoCaoTonDTO chiTietBaoCao)
      {
          ChiTietBaoCaoTon ct = new ChiTietBaoCaoTon()
          {
              MaBaoCaoTon=chiTietBaoCao.MaBaoCaoTon,
              MaSach=chiTietBaoCao.MaSach,
              TonDau=chiTietBaoCao.TonDau,
              PhatSinh=chiTietBaoCao.PhatSinh,
              TonCuoi=chiTietBaoCao.TonCuoi
          };
          NhaSachEntities context = new NhaSachEntities();
          context.AddToChiTietBaoCaoTonList(ct);
          context.SaveChanges();
      }
      public static void Update(ChiTietBaoCaoTonDTO chiTietBaoCao)
      {
          NhaSachEntities context = new NhaSachEntities();
          var chiTiet = (from ct in context.ChiTietBaoCaoTonList
                         where chiTietBaoCao.MaChiTiet == ct.MaChiTiet
                         select ct).FirstOrDefault();
          chiTiet.TonDau = chiTietBaoCao.TonDau;
          chiTiet.PhatSinh = chiTietBaoCao.PhatSinh;
          chiTiet.TonCuoi = chiTietBaoCao.TonCuoi;

          context.SaveChanges();
      }
    }
}
